﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.ComponentModel;

namespace Components
{
    [System.ComponentModel.DefaultBindingProperty("DateID")]
    public class SGVDateTime : SGVDateTimePicker, INotifyPropertyChanged 
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public SGVDateTime()
            : base()
        {
            DateID = Convert.ToInt32(DateTime.Now.ToString("yyyyMMdd"));
            _bindingPropertyName = "DateID";
            this.ValueChanged += new EventHandler(SGVDateTime_ValueChanged);
            this.TextChanged += new EventHandler(SGVDateTime_TextChanged);
        }

        void SGVDateTime_TextChanged(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(this.Text))
            {
                this._dateID = 0;
            }
        }

        void SGVDateTime_ValueChanged(object sender, EventArgs e)
        {
            DateTime dt = this.Value;
            _dateID = Convert.ToInt32(dt.ToString("yyyyMMdd"));
            if(PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs("DateID"));
        }

        int _dateID;

        public int DateID
        {
            get { return _dateID; }
            set 
            { 
                _dateID = value;
                if (_dateID == 0)
                {
                    this.Text = String.Empty;
                }
                else
                {
                    DateTime dt = DateTime.ParseExact(_dateID.ToString(), "yyyyMMdd", System.Globalization.CultureInfo.CurrentCulture);
                    this.Value = dt;
                }
            }
        }


    }
}
